const ajax = require('ajax');
const common = require('common');
cc.Class({
    extends: cc.Component,

    properties: {
        remotebookface: cc.Prefab,
    },

    // use this for initialization
    onLoad: function() {


    },
    init: function() {
        let self = this;
        self.node.removeAllChildren(true);
        cc.log('---------bookstorecontent init ------');
        ajax.getJSON('http://git.oschina.net/shishi11shf/picturebook/raw/master/assets/script/bookstore.json', { //http://odxmd36pu.bkt.clouddn.com/bookstore.json', {
            a: '2'
        }, function(data) {
            // self.remotebookjson = data;
            let booklocal = data[0].books;
            //这是临时的

            booklocal = booklocal.concat(data[1].books);
            booklocal = booklocal.concat(data[2].books);
            cc.log('booklocal:   ' + booklocal + booklocal.length);
            //此时的common的bookdata可能还没有加载进来
            let bookdata = common.init.bookdata;
            for (var i = booklocal.length - 1; i >= 0; i--) {
                let defaultbookface = cc.instantiate(self.remotebookface);
                //这里要判断一下是否在本地有？
                let flag = true;
                for (var n = 0; n < bookdata.length; n++) {
                    if (bookdata[n].id === booklocal[i].id) {
                        flag = false;
                    }
                }
                //cc.log(bookface.getComponent('bookface'));
                let bookfacescript = defaultbookface.getComponent('defaultbookface');
                bookfacescript.setBookName(booklocal[i].bname);
                if (booklocal[i].img !== '') {
                    cc.log('booklocal[i].img' + booklocal[i].img);
                    bookfacescript.setBookFace(booklocal[i].img);
                }
                bookfacescript.setBtn(booklocal[i], !flag);
                defaultbookface.parent = self.node;

            }
        });
    }


});